# coding:utf8

import os
import tornado.ioloop
import tornado.log
import tornado.web
import tornado.wsgi
import tornado.options
import logging
from tornado.options import OptionParser

# from raven import Client
# from raven.conf import setup_logging
# from raven.handlers.logging import SentryHandler

import config
import controller
import api_ctrl
import back_ctrl
import wx_ctrl
# client = Client(config.gconf['sentry_dsn'])
# handler = SentryHandler(client)
# setup_logging(handler)

settings = dict(
    blog_title=u"Tornado Blog",
    template_path=os.path.join(os.path.dirname(__file__), "views"),
    static_path=os.path.join(os.path.dirname(__file__), "static"),
    # ui_modules={"Entry": EntryModule},
    # xsrf_cookies=True,
    cookie_secret=config.cookie_secret,
    login_url="/wx/login",

    debug=False if config.run_mode == 'release' else True
)


def make_app():
    tornado.options.options.log_to_stderr = True
    tornado.options.options.logging = 'debug'
    tornado.options.options.log_file_prefix = config.srv['log']['application.log']
    tornado.log.enable_pretty_logging(tornado.options.options, tornado.log.app_log)

    application = tornado.web.Application(controller.urls.ctrls + api_ctrl.urls.ctrls + back_ctrl.urls.ctrls + wx_ctrl.urls.ctrls,
                                          **settings)
    return application


wsgiapp = tornado.wsgi.WSGIAdapter(make_app())
